package com.storeroompda.ui.config;

import android.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull;
import android.util.Log;

import com.storeroompda.App;
import com.storeroompda.base.model.BaseModel;
import com.storeroompda.utils.Utils;
import com.vilyever.socketclient.SocketClient;
import com.vilyever.socketclient.helper.SocketClientDelegate;
import com.vilyever.socketclient.helper.SocketResponsePacket;
import com.vilyever.socketclient.util.CharsetUtil;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ConfigModelImpl extends BaseModel implements ConfigModel {
    private String tag = "ConfigModelImpl";
    private String msg="";
    SocketClient socketClient ;

    public void postHandle(final CallBack listener, final int n) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                switch (n) {
                    case -1:
                        listener.onFail(msg);
                        break;
                    case 0:
                        listener.onconfigSuccess();
                        break;
                    default:
                        break;
                }
            }
        });
    }

    @Override
    public void config(final String address, final String port, final CallBack listener) {
        try {
            socketClient = new SocketClient();
            socketClient.getAddress().setRemoteIP(address);
            socketClient.getAddress().setRemotePort(Integer.parseInt(port));
            socketClient.getAddress().setConnectionTimeout(10 * 1000);
            socketClient.setCharsetName(CharsetUtil.UTF_8);
            socketClient.registerSocketClientDelegate(new SocketClientDelegate() {
                @Override
                public void onConnected(SocketClient client) {
                    Log.e(tag,"onConnected");
                    App.sEditor.putString(Utils.addresskey,address);
                    App.sEditor.putString(Utils.portkey,port);
                    App.sEditor.commit();
                    postHandle(listener,0);
                }
                @Override
                public void onDisconnected(SocketClient client) {
                    Log.e(tag,"onDisconnected");
                    App.sEditor.putString(Utils.addresskey,"");
                    App.sEditor.putString(Utils.portkey,"");
                    App.sEditor.commit();
                    msg=Utils.netConnectionError;
                    postHandle(listener,-1);
                }
                @Override
                public void onResponse(final SocketClient client, @NonNull SocketResponsePacket responsePacket) {
                    Log.e(tag,"onResponse:"+responsePacket.getMessage());
                }
            });
//            if(socketClient.getState() == SocketClient.State.Connected)
//            {
//                socketClient.connect();
//            }
//            else
//            {
//                socketClient.connect();
//            }
            socketClient.connect();
        } catch (Exception e) {
            e.printStackTrace();
            msg=Utils.exceptionError;
            postHandle(listener, -1);
        }
    }

    @Override
    public void disconnect(CallBack listener) {
        socketClient=null;
    }
}

